<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title>扫描结果</title>
		<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
		<meta name="format-detection" content="telephone=no, address=no, email=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="css/mui.min.css" type="text/css" charset="utf-8" />
		<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
		<style> .mui-content { width: 93.6%; position: absolute; top: 45%; left: 50%; transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); }  p.scanStatus { font-size: 48px; margin-top: 0.5rem; }  .error p.scanStatus { font-size: 32px; }  .content { text-align: left; margin-top: 1rem; background: #fff; padding: 0.15rem; }  .error .content { margin-left: 1.7rem; }  .content p { font-size: 30px; }  @media screen and (max-width:375px) { p.scanStatus { font-size: 24px; } .error p.scanStatus { font-size: 20px; } .content p { font-size: 16px; } }  @media screen and (min-width:376px) and (max-width:414px) { p.scanStatus { font-size: 28px; } .error p.scanStatus { font-size: 22px; } .content p { font-size: 18px; } }  @media screen and (min-width:415px) and (max-width:639px) { p.scanStatus { font-size: 32px; } .error p.scanStatus { font-size: 24px; } .content p { font-size: 20px; } }  @media screen and (min-width:640px) and (max-width:719px) { p.scanStatus { font-size: 40px; } .error p.scanStatus { font-size: 26px; } .content p { font-size: 26px; } }  @media screen and (min-width:720px) and (max-width:749px) { p.scanStatus { font-size: 44px; } .error p.scanStatus { font-size: 28px; } .content p { font-size: 28px; } }  @media screen and (min-width:750px) and (max-width:799px) { p.scanStatus { font-size: 48px; } .error p.scanStatus { font-size: 32px; } .content p { font-size: 30px; } }  .none { display: none; } </style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav mui-header">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">扫码结果</h1>
		</header>

		<div class="mui-content text-center">
			<div class="success">
				<img src="" id="statusImg" style="width:1.92rem" />
				<p class="text-center colorblue scanStatus" id="scanStatus"></p>
				<div class="content" id="content" style="display: none;">

				</div>
			</div>
		</div>

		<div class="mui-bar" style="bottom:0.26rem;background:transparent;border:none;">
			<button class="mui-btn mui-hidden" style="background:#f1c40f;border-color:#f1c40f;" id="finish">完成</button>
			<button class="mui-btn mui-hidden" style="background:#32b16c;border-color:#32b16c;margin-left:2%;" id="continue">继续</button>
		</div>

		<script src="js/resize.js"></script>
		<script src="js/mui.min.js"></script>
		<script src="js/ajaxFun.js"></script>
		<script src="js/dialog.js"></script> 
		<script>
			var data = JSON.parse(localStorage.getItem("scanResult") || '[]');
			var users = JSON.parse(localStorage.getItem("$users") || '[]');
			var continueObj = document.getElementById("continue");
			var finishObj = document.getElementById("finish");
			var pname, pimg, totalCount=0,source;

			/*扫描成功时候执行的函数
			 *type为扫描码的类型
			 * result为扫描出的二维码的的url
			 * flag 1表示出库 2表示从入库扫描
			 */
			function　 scaned(type, result, flag) {
				plus.webview.currentWebview().show('slide-in-right');
				data.flag=flag;
				localStorage.setItem("scanResult", JSON.stringify(data));
				data.boxNo = result;
				var totalUrl = flag == 1 ? baseUrl + 'outScan/addOutScanData.do' : flag == 2 ? baseUrl + 'in/addInScanData.do' : '';
				//这里可以根据传来的参数获取数据
				mui.plusReady(function() {
					setTimeout(function(){
						if(totalUrl) {
							ajaxFun.postFun(totalUrl, data, '', function(d) {
								document.getElementById("scanStatus").innerHTML = '采集成功';
								document.getElementById("statusImg").src = 'img/login/scansuccess.png';
								if(d) {
									formHtml(d);
								}
								/*完成扫码*/
								finishObj.addEventListener("tap", finishFun);
							}, function(d) {
								document.getElementById("scanStatus").innerHTML = d.msg;
								document.getElementById("statusImg").src = 'img/login/scanerror.png';
								formHtml(d, 2);
								if(parseInt(d.data.count) > 0) {
									finishObj.style.display = 'inline-block';
									finishObj.addEventListener("tap", finishFun);/*完成扫码*/
								} else {
									finishObj.style.backgroundColor = '#ddd';
									finishObj.style.color = "#666";
									finishObj.style.borderColor = "#ddd";
									finishObj.removeEventListener('tap', finishFun);
								}
								
							},function(){
								localStorage.clear();
								openWindow('./login.html','login');
							})
						} else {
							plus.nativeUI.alert('请选择要扫描的二维码');
						}	
					},0)
				})
			}

			function formHtml(d, ss) {
				console.log(d);
				if(!d.data){
					plus.nativeUI.alert(d.msg);
					return false;
				}
				pname = d.data.productName ? d.data.productName : '';
				pimg = d.data.imgUrl;
				source=d.data.sourceName;
				var newData;
				totalCount = d.data.totalCount? (d.data.totalCount>= 0 ? d.data.totalCount : 0 ) : 0;
				newData = {
					'boxNo': data.boxNo,
					count: d.data.count,
					inNo: d.data.inNo,
					outNo: d.data.outNo,
					flag: data.flag,
					totalCount: totalCount,
					productNo:d.data.productNo,
				}
				if(totalCount==d.data.count && totalCount!=0){
					addClass(continueObj,'mui-hidden');
					finishObj.style.width='100%'; finishObj.style.margin='0';removeClass(finishObj,'mui-hidden');
				}else{
					removeClass(continueObj,'mui-hidden');
					removeClass(finishObj,'mui-hidden');				
				}
								
				if(ss!= 2) {
					localStorage.setItem("scanResult", JSON.stringify(newData)); //设置当前到缓存中区		
				}
				newData.boxNo=newData.boxNo?newData.boxNo:'';
				newData.count=newData.count?newData.count:0;
				d.data.productName=d.data.productName?d.data.productName:'';
				newData.count = countUnit(newData.count); //格式化单位
				var count = data.flag == 1 ? newData.count + '箱' : data.flag == 2 ? newData.count + ' / ' + totalCount + '箱' : '';
				var html = '<p>箱码：<span id="boxNo">' + newData.boxNo + '</span></p> <p style="overflow:hidden"><span style="width:28%;float:left;display:inline-block">产品名称：</span><span id="pname"  style="width:72%;float:left;display:inline-block">' + d.data.productName + '</span></p> ';
				html += '<p>已采集：<span id="pcount">' + count + '</span></p>';
				document.getElementById("content").innerHTML = html;
				document.getElementById("content").style.display = 'block';
				if(d.msg == '产品不同，采集失败') {
					document.getElementById("pname").style.color = '#FF0000';
				}
			}

			mui.plusReady(function() {
				var curr = plus.webview.currentWebview();
				var mainview = plus.webview.getWebviewById('main-inout');
				var scanview = plus.webview.getWebviewById('barcode_scan');
				curr.addEventListener('show', function() {
					if(scanview) {
						scanview.close();
					}
				});
				/*继续扫码*/
				continueObj.addEventListener("tap", function() {
					openWindow('barcode_scan.html', 'barcode_scan', {flag:data.flag})
				})

				mui.init({
					beforeback: function() {
						var wobj = plus.webview.getWebviewById("main-inout");
									if(!wobj){
										wobj= plus.webview.create("app/inout/index.html","main-inout");
									}
									mui.fire(wobj, 'flagParam', {
										flagParam: data.flag
									});
//									wobj.show();		
					}
				});

			});

			var old_back=mui.back;
			mui.back=function() {
				var dialogObj=new DialogObj();
				dialogObj.showConfirm('退出扫码','确定退出吗？当前扫码进度将被保留',{
							yes:{
								'msg': '确定',
								'callback': function(){
									var a=plus.webview.getWebviewById('main-inout');
									a.show();
									mui.fire(a,'flagParam',{
										flagParam:data.flag
									});
									return true;
								},
								'isexist':true,
							},
							no:{
								'msg': '取消',
								'callback': function(){
									document.getElementsByTagName('body')[0].removeChild(dialogObj.currentDialogObj); 
									return false;
								},
								'isexist':true,
							}
						});
				 
			 }
			
			function finishFun() {
				openWindow('scan-finish.html', 'scan-finish', {
					pname: pname,
					pimg: pimg,
					totalCount: totalCount,
					source:source,
				})
			}
		</script>

	</body>

</html>